package server.handler;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.json.JSONArray;
import org.json.JSONObject;
import server.database.CoordDatabase;
import server.entity.Coordinate;

import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class QueryHandler implements HttpHandler {
    private CoordDatabase db;
    private Integer maxDataLen = 1024 * 1024;
    public QueryHandler() {
        db = CoordDatabase.getInstance();
    }

    @Override
    public void handle(HttpExchange exchange) throws IOException {

        Headers headers = exchange.getRequestHeaders();

        String contentType = headers.get("Content-Type").get(0);
        if(contentType.equalsIgnoreCase("application/json")){
            if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
//                if(!Handler.authentication(exchange)){
//                    Handler.errorHandler(exchange, "Please login or register first");
//                }
                String inputData;
                try{
                    inputData = Handler.getRequestData(exchange);
                }catch (IOException e){
                    Handler.errorHandler(exchange, e);
                    return;
                }
                JSONObject jsonObject = new JSONObject(inputData);
                if(!jsonObject.has("query")){
                    Handler.errorHandler(exchange, "query method is require");
                    return;
                }
                String method = jsonObject.getString("query");

                if(method.equals("user")){
                    if(!jsonObject.has("nickname")){
                        Handler.errorHandler(exchange, "nickname is require");
                        return;
                    }
                    try {

                        JSONArray response = db.getCoordinatesByUser(jsonObject.getString("nickname"));
                        Handler.sendResponse(exchange, response);
                    } catch (SQLException e) {
                        Handler.errorHandler(exchange, e);
                    }
                }
                else if(method.equals("time")){
                    if(!jsonObject.has("timestart")){
                        Handler.errorHandler(exchange, "start time is require");
                        return;
                    }
                    if(!jsonObject.has("timeend")){
                        Handler.errorHandler(exchange, "end time is require");
                        return;
                    }
                    try {

                        LocalDateTime timeStart = Handler.localDateTimeToEpochMilli(jsonObject.getString("timestart"));
                        LocalDateTime timeEnd = Handler.localDateTimeToEpochMilli(jsonObject.getString("timeend"));
                        if(timeEnd.isBefore(timeStart)){
                            Handler.errorHandler(exchange, "Start time should be less than end time");
                            return;
                        }
                        JSONArray response = db.getCoordinatesByTime(timeStart, timeEnd);
                        Handler.sendResponse(exchange, response);
                    } catch (SQLException e) {
                        Handler.errorHandler(exchange, e);
                    }
                }
            }else{
                //Code for handling other requests
                Handler.errorHandler(exchange, "Unsupported request method");
            }
        }
        else {
            Handler.errorHandler(exchange, "Unsupported request contentType");
        }
    }
}

